<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;

use App\Models\DeliveryService;
use App\Models\WechatUser;
use App\Traits\Controllers\AdminTraits;
use App\Traits\Controllers\UpdateStatusTraits;
use App\Transformers\DeliveryServiceTransFormer;
use App\Transformers\Wechat\WechatUserTransFormer;
use Illuminate\Http\Request;

class DeliveryServiceController extends Controller
{

    /**
     *配送员表的 增删改查
     */
    use AdminTraits;
    use UpdateStatusTraits;

    public $model = DeliveryService::class;

    public $transformer = DeliveryServiceTransFormer::class;

    public function  deliveryUpdate(DeliveryService $deliveryService, Request $request)
    {
        $deliveryService->update($request->all());
        $deliveryService->save();
        return $this->response->noContent();
    }

    //返回所有微信用户的信息
    public function wechat(){
        $wechat = WechatUser::query()
            ->pimp()
            ->paginate($this->getPageLimit());

        return $this->response->paginator($wechat,new WechatUserTransFormer());
    }


    /**
     * 参数验证规则
     *
     * @param Request $request
     * @return array
     */
    public function rules(Request $request): array
    {
        switch ($request->method()) {
            case 'GET':
                return [];
                break;
            case 'POST':
            case 'PUT':
                return [
                    'avatar' => 'required',
                    'nickname' => 'required|exists:wechat_users',
                    'phone' => 'required|regex:/^1[3-9]\d{9}$/|unique',
                    'status' => 'required',
                    'name'=>'required'

                ];
                break;
            case 'PATCH':
                return [
                    'status' => 'required'
                ];
                break;

        }
        return [];
    }

    /**
     * 验证信息
     *
     * @return array
     */
    public function ruleMessages(): array
    {
        return [
     'avatar.required'=>'头像不能为空',
            'nickname.required'=>'昵称不能为空',
            'nickname.exist'=>'昵称不存在',
            'phone.required'=>'手机号不能为空',
            'phone.regex'=>'手机号码格式不正确',
            'phone.unique'=>'手机号不能重复添加',
            'status.required'=>'状态不能为空',
            'name.required'=>'名称不能为空'
        ];
    }

}
